package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import com.sgb.kjwl.R;
import com.sgb.link.view.adapter.CommonCategoryFilterPopAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

public class CommonCategoryFilterPopWindow extends BasePopWindow {

    /**
     * 三级标题，通过category不同显示的不同
     *
     */
    // 2, "劳务分包" 3, "商品/材料" 4, "专业工程" 6, "勘察设计" 7, "建筑设计" 8, "工程监理" 9, "工程造价咨询" 10, "工程总承包"
    // 1, "机械租赁" 5, "设备租赁" 11, "房产租赁" 12, "车辆租赁"
    //"1"  机械设备租赁 "2"  劳务分包 "3"  商品材料 "4"  专业工程  竞价
    private String[][] titles = new String[][]{
            new String[]{"机械分类", "机械类型"},
            new String[]{"工程类别", "专业分类", "业务类别"},
            new String[]{"商品分类", "商品类别", "商品品名"},
            new String[]{"工程类别", "专业分类", "业务类型"},
            new String[]{"设备分类", "设备类型"},
            new String[]{"房产类型", "房产分类"},
            new String[]{"货物类型", "要求车型"},
    };

    private CommonCategoryFilterPopAdapter categoryAdapter;
    private CommonCategoryFilterPopAdapter secondAdapter;
    private CommonCategoryFilterPopAdapter thirdAdapter;
    private CommonCategoryFilterPopAdapter typeAdapter;

    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> categoryList = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity>>> secondList = new ArrayList<>();
    private List<PickSelectEntity> thirdList = new ArrayList<>();
    private List<PickSelectEntity> typeList = new ArrayList<>();

    private Listener listener;
    private String typeNo = "0";
    private String categoryNo;
    private String categorySecondNo;
    private String categoryThirdNo;

    private String text = "全部";
    private String goodsType = "";
    private int category = -1;
    private int tabPosition = -1;

    private PickSelectEntity categoryFirstEntity;
    private PickSelectEntity categorySecondEntity;
    private TextView tv_reset;
    private TextView tvSure;
    private boolean isBid;

    public CommonCategoryFilterPopWindow(Context context) {
        super(context);
    }

    public CommonCategoryFilterPopWindow(Context context,boolean isAttention) {
        super(context);
        this.isAttention = isAttention;
        initData();
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_common_category_filter;
    }

    private TextView tvType;
    private TextView tvCategory;
    private TextView tvSecondCategory;
    private TextView tvThirdCategory;
    private MaxRecyclerView typeListView;
    private MaxRecyclerView categoryListView;
    private MaxRecyclerView secondListView;
    private MaxRecyclerView thirdListView;
    private LinearLayout llSecondCategory;
    private LinearLayout llThirdCategory;
    private LinearLayout llCategory;
    private LinearLayout llType;
    private boolean isAttention;

    private int pos1 = 0, pos2 = -1, pos3 = -1, pos4 = -1;
    private String posName1 = "", posName2 = "", posName3 = "", posName4 = "";

    @Override
    protected void initView(PopViewHolder holder) {
        tvType = holder.getPop().findViewById(R.id.tv_type);
        tvCategory = holder.getPop().findViewById(R.id.tv_category);
        tvSecondCategory = holder.getPop().findViewById(R.id.tv_second_category);
        tvThirdCategory = holder.getPop().findViewById(R.id.tv_third_category);

        tv_reset = holder.getPop().findViewById(R.id.reset_step);
        tvSure = holder.getPop().findViewById(R.id.sure_step);

        typeListView = holder.getPop().findViewById(R.id.type_list);
        categoryListView = holder.getPop().findViewById(R.id.category_list);
        secondListView = holder.getPop().findViewById(R.id.second_list);
        thirdListView = holder.getPop().findViewById(R.id.third_list);

        llSecondCategory = holder.getPop().findViewById(R.id.ll_second_category);
        llThirdCategory = holder.getPop().findViewById(R.id.ll_third_category);
        llCategory = holder.getPop().findViewById(R.id.ll_category);
        llType = holder.getPop().findViewById(R.id.ll_type);

    }

    private void initData(){
        if (isAttention) {
            initType();
            llType.setVisibility(View.VISIBLE);
            llCategory.setVisibility(View.GONE);
        } else {
            llType.setVisibility(View.GONE);
            llCategory.setVisibility(View.VISIBLE);
        }

        initCategory();
        initSecondCategory();
        initThirdCategory();

        //重置
        tv_reset.setOnClickListener(view -> {
            if(isAttention){
                llCategory.setVisibility(View.GONE);
                llSecondCategory.setVisibility(View.GONE);

                typeAdapter.resetCheckDatas();
                typeAdapter.setDefItem(0);
                categoryAdapter.resetCheckDatas();
                categoryAdapter.setDefItem(0);

                typeNo = "";
                categoryNo = "";
                pos1 = 0;
                pos4 = -1;
            }

            categorySecondNo = "";
            categoryThirdNo = "";

            llThirdCategory.setVisibility(View.GONE);
            secondAdapter.resetCheckDatas();
            secondAdapter.setDefItem(0);
            thirdAdapter.resetCheckDatas();
            thirdAdapter.setDefItem(0);

            text = "全部";
            goodsType = "";

            pos2 = -1;
            pos3 = -1;


            if (null != listener) {
                listener.setResetResultListener();
            }
            //dissMiss();
        });
        //确定
        tvSure.setOnClickListener(view -> {
            if (null != listener) {
                listener.setResultListener(text,goodsType,typeNo);
            }
            if (mCategoryCallBack != null) {
                mCategoryCallBack.resultPosition(posName1, posName2, posName3, pos1, pos2, pos3);
            }
            if (mCategoryCallBack1 != null) {
                mCategoryCallBack1.resultPosition1(posName1, posName2, posName3,posName4, pos1, pos2, pos3,pos4);
            }
            dissMiss();
        });
    }
    private void initType() {
        typeAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeListView.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeListView.setHasFixedSize(false);
        typeListView.setAdapter(typeAdapter);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(0);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity>> pickSelectEntity, int position) {
                pos4 = position;
                pos1 = 0;
                pos2 = 0;
                pos3 = 0;
                if (pickSelectEntity.getNo().equals(typeNo)) {
                    return;
                }
                if (pickSelectEntity.getProjectscale().equals("全部")) { //全部按钮
                    text = pickSelectEntity.getPickerViewText();
                    typeNo = "0";
                    goodsType = "0";

                    llCategory.setVisibility(View.GONE);
                    llSecondCategory.setVisibility(View.GONE);
                    llThirdCategory.setVisibility(View.GONE);

                    categoryAdapter.resetCheckDatas();
                    categoryAdapter.setDefItem(0);
                    secondAdapter.resetCheckDatas();
                    secondAdapter.setDefItem(0);
                    thirdAdapter.resetCheckDatas();
                    thirdAdapter.setDefItem(0);
                    return;
                }
                text = pickSelectEntity.getPickerViewText();
                typeNo = "0";
                goodsType = "0";

                llCategory.setVisibility(View.GONE);
                llSecondCategory.setVisibility(View.GONE);
                llThirdCategory.setVisibility(View.GONE);

                categoryList.clear();
                secondList.clear();
                thirdList.clear();
                categoryAdapter.resetCheckDatas();
                categoryAdapter.setDefItem(0);
                secondAdapter.resetCheckDatas();
                secondAdapter.setDefItem(0);
                thirdAdapter.resetCheckDatas();
                thirdAdapter.setDefItem(0);

                if (pickSelectEntity == null) {
                    return;
                }
                typeNo = pickSelectEntity.getNo();
                if(!TextUtils.isEmpty(typeNo)){
                    category = Integer.parseInt(typeNo);
                }
                setTitles(pickSelectEntity.getProjectscale());
                mCategoryCallBack1.setChildData(typeNo);
            }
        });

    }

    private void initCategory() {
        categoryAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        categoryListView.setLayoutManager(new GridLayoutManager(mContext, 4));
        categoryListView.setHasFixedSize(false);
        categoryListView.setAdapter(categoryAdapter);
        categoryAdapter.setItemSelect(true);
        categoryAdapter.setDefItem(0);
        categoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>> pickSelectEntity, int position) {
                categoryFirstEntity = pickSelectEntity;
                pos1 = position;
                pos2 = 0;
                pos3 = 0;
                if (pickSelectEntity.getNo().equals(categoryNo)) {
                    return;
                }
                if (pickSelectEntity.getNo().equals("")) { //全部按钮
                    text = pickSelectEntity.getPickerViewText();
                    goodsType = pickSelectEntity.getNo();
                    categoryNo = null;

                    llSecondCategory.setVisibility(View.GONE);
                    llThirdCategory.setVisibility(View.GONE);

                    secondAdapter.resetCheckDatas();
                    secondAdapter.setDefItem(0);

                    thirdAdapter.resetCheckDatas();
                    thirdAdapter.setDefItem(0);
                    return;
                }

                text = pickSelectEntity.getPickerViewText();
                goodsType = pickSelectEntity.getNo();

                llSecondCategory.setVisibility(View.GONE);
                llThirdCategory.setVisibility(View.GONE);

                secondList.clear();
                thirdList.clear();
                secondAdapter.resetCheckDatas();
                secondAdapter.setDefItem(0);
                thirdAdapter.resetCheckDatas();
                thirdAdapter.setDefItem(0);

                if (pickSelectEntity.getChildren() == null || pickSelectEntity.getChildren().size() == 0) {
                    return;
                }

                secondList.addAll(pickSelectEntity.getChildren());
                secondAdapter.setDatas(secondList);
                llSecondCategory.setVisibility(View.VISIBLE);
                if("机械/设备".equals(tvCategory.getText().toString())){
                    if (pos1 == 1) {
                        tvSecondCategory.setText("机械分类");
                    } else if (pos1 == 2) {
                        tvSecondCategory.setText("设备分类");
                    }
                }
                categoryNo = pickSelectEntity.getNo();
            }
        });

    }

    private void initSecondCategory() {
        secondAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        secondListView.setLayoutManager(new GridLayoutManager(mContext, 4));
        secondListView.setHasFixedSize(false);
        secondListView.setAdapter(secondAdapter);
        secondAdapter.setItemSelect(true);
        secondAdapter.setDefItem(0);
        secondAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity>> pickSelectEntity, int position) {
                categorySecondEntity = pickSelectEntity;
                pos2 = position;
                pos3 = 0;
                if (pickSelectEntity.getNo().equals(categorySecondNo)) {
                    return;
                }
                if (pickSelectEntity.getNo().equals("")) {//全部
                    llThirdCategory.setVisibility(View.GONE);
                    thirdAdapter.resetCheckDatas();
                    thirdAdapter.setDefItem(0);
                    if(isAttention){
                        text = categoryFirstEntity.getPickerViewText();
                        goodsType = categoryFirstEntity.getNo();
                    }
                    return;
                }
                text = pickSelectEntity.getPickerViewText();
                goodsType = pickSelectEntity.getNo();

                llThirdCategory.setVisibility(View.GONE);
                thirdList.clear();

                thirdAdapter.resetCheckDatas();
                thirdAdapter.setDefItem(0);

                categorySecondNo = pickSelectEntity.getNo();

                if (pickSelectEntity.getChildren() == null || pickSelectEntity.getChildren().size() == 0) {
                    return;
                }

                thirdList.addAll(pickSelectEntity.getChildren());
                thirdAdapter.setDatas(thirdList);
                llThirdCategory.setVisibility(View.VISIBLE);
                if("机械/设备".equals(tvCategory.getText().toString())){
                    if (pos1 == 1) {
                        tvThirdCategory.setText("机械类型");
                    } else if (pos1 == 2) {
                        tvThirdCategory.setText("设备类型");
                    }
                }

            }
        });
    }

    private void initThirdCategory() {
        thirdAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        thirdListView.setLayoutManager(new GridLayoutManager(mContext, 4));
        thirdListView.setHasFixedSize(false);
        thirdListView.setAdapter(thirdAdapter);
        thirdAdapter.setItemSelect(true);
        thirdAdapter.setDefItem(0);
        thirdAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pos3 = position;
                if (pickSelectEntity.getNo().equals(categoryThirdNo)) {
                    return;
                }
                if (pickSelectEntity.getNo().equals("")) {
                    text = categorySecondEntity.getPickerViewText();
                    goodsType = categorySecondEntity.getNo();
                    return;
                }
                text = pickSelectEntity.getPickerViewText();
                goodsType = pickSelectEntity.getNo();
                categoryThirdNo = pickSelectEntity.getNo();
            }
        });
    }

    private void initTitle(int category,String categoryName) {
//        category -= 1;
//        if (category >= titles.length) {
//            category = titles.length - 1;
//        }
        if (category < 0) {
            this.category = 0;
        }
        this.category = category;

        if(isAttention){
            if (isBid) {
                posName4 = "竞价类型";
            } else{
                posName4 = "招标类型";
            }
            tvType.setText(posName4);
        }else{
            llType.setVisibility(View.GONE);
            llCategory.setVisibility(View.VISIBLE);
        }
        setTitles(categoryName);
    }

    private void initTitle1(int category,String categoryName) {
        if (category < 0) {
            this.category = 0;
        }
        this.category = category;

        llType.setVisibility(View.GONE);
        llCategory.setVisibility(View.GONE);
        llSecondCategory.setVisibility(View.VISIBLE);

        setTitles(categoryName);
    }

    private void setTitles(String categoryName){
        if (category == 6 || category == 7 || category == 8 || category == 9 || category == 10) {
            tvCategory.setText(titles[3][0]);
            tvSecondCategory.setText(titles[3][1]);
            tvThirdCategory.setText(titles[3][2]);
        } else if (category == 11 || category == 15) {
            tvCategory.setText(titles[5][0]);
            tvSecondCategory.setText(titles[5][1]);
        } else if (category == 12 || category == 18) {
            tvCategory.setText("车型");
        } else if (category == 13) {
            tvCategory.setText("工程类别");
        } else if (category == 14) {
            tvCategory.setText("土地用途");
        } else if (category == 16) {
            tvCategory.setText(titles[0][0]);
            tvSecondCategory.setText(titles[0][1]);
        } else if (category == 17) {
            tvCategory.setText(titles[4][0]);
            tvSecondCategory.setText(titles[4][1]);
        } else if (category == 19) {
            tvCategory.setText(titles[6][0]);
            tvSecondCategory.setText(titles[6][1]);
        } else {
            if (category > 0 && category != 20) {
                tvCategory.setText(titles[category - 1][0]);
                tvSecondCategory.setText(titles[category - 1][1]);
                if (titles[category - 1].length == 3) {
                    if (secondList != null) {
                        tvThirdCategory.setText(titles[category - 1][2]);
                    }
                }
            } else if (category == 20) {
                tvCategory.setText("");
            }
//            if ("机械设备租赁".equals(categoryName)) {
//                tvCategory.setText("机械/设备");
//                if (tabPosition == 1) {
//                    tvSecondCategory.setText(titles[0][0]);
//                    tvThirdCategory.setText(titles[0][1]);
//                } else if (tabPosition == 2) {
//                    tvSecondCategory.setText(titles[4][0]);
//                    tvThirdCategory.setText(titles[4][1]);
//                }
//            } else {
//                if (category > 0 && category != 20) {
//                    tvCategory.setText(titles[category - 1][0]);
//                    tvSecondCategory.setText(titles[category - 1][1]);
//                    if (titles[category - 1].length == 3) {
//                        if (secondList != null) {
//                            tvThirdCategory.setText(titles[category - 1][2]);
//                        }
//                    }
//                } else if (category == 20) {
//                    tvCategory.setText("");
//                }
//            }
        }
        posName1 = tvCategory.getText().toString();
        posName2 = tvSecondCategory.getText().toString();
        posName3 = tvThirdCategory.getText().toString();
    }

    public void setData1(List<PickSelectEntity> pickSelectEntities, int category,String categoryName,boolean isBid) {
        if (this.category != category) {

            typeList.clear();
            typeList.addAll(pickSelectEntities);
            typeAdapter.setDatas(typeList);
            this.isBid = isBid;
            initTitle(category,categoryName);

            categoryList.clear();
            secondAdapter.clearDatas();
            thirdAdapter.clearDatas();

            typeAdapter.resetCheckDatas();
            typeAdapter.setDefItem(0);
            categoryAdapter.resetCheckDatas();
            categoryAdapter.setDefItem(0);
            secondAdapter.resetCheckDatas();
            secondAdapter.setDefItem(0);
            thirdAdapter.resetCheckDatas();
            thirdAdapter.setDefItem(0);

            llCategory.setVisibility(View.GONE);
            llSecondCategory.setVisibility(View.GONE);
            llThirdCategory.setVisibility(View.GONE);
            text = "全部";
            goodsType = "";
            this.category = category;
        }
    }

    public void setData(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> pickSelectEntities, int category,String categoryName) {
        if (this.category != category) {
            initTitle(category,categoryName);
            categoryList.clear();
            categoryList.addAll(pickSelectEntities);
            categoryAdapter.setDatas(categoryList);
            secondAdapter.clearDatas();
            thirdAdapter.clearDatas();

            categoryAdapter.resetCheckDatas();
            categoryAdapter.setDefItem(0);
            secondAdapter.resetCheckDatas();
            secondAdapter.setDefItem(0);
            thirdAdapter.resetCheckDatas();
            thirdAdapter.setDefItem(0);

            llSecondCategory.setVisibility(View.GONE);
            llThirdCategory.setVisibility(View.GONE);
            text = "全部";
            goodsType = "";
            this.category = category;
        }
    }

    public void setData2(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntities, int category,String categoryName) {
        initTitle1(category,categoryName);
        secondList.clear();
        secondList.addAll(pickSelectEntities);
        secondAdapter.setDatas(secondList);
        thirdAdapter.clearDatas();

        secondAdapter.resetCheckDatas();
        secondAdapter.setDefItem(0);
        thirdAdapter.resetCheckDatas();
        thirdAdapter.setDefItem(0);

        llThirdCategory.setVisibility(View.GONE);
        text = "全部";
        goodsType = "";
        this.category = category;
    }

    public void setData2(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntities, int category,String categoryName,int tabPosition) {
        this.tabPosition = tabPosition;
        initTitle1(category,categoryName);
        secondList.clear();
        secondList.addAll(pickSelectEntities);
        secondAdapter.setDatas(secondList);
        thirdAdapter.clearDatas();

        secondAdapter.resetCheckDatas();
        secondAdapter.setDefItem(0);
        thirdAdapter.resetCheckDatas();
        thirdAdapter.setDefItem(0);

        llThirdCategory.setVisibility(View.GONE);
        text = "全部";
        goodsType = "";
        this.category = category;
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }


    public interface Listener {
        void setResultListener(String text, String result,String typeNo);
        void setResetResultListener();
    }

    public void setResultListener(Listener l) {
        this.listener = l;
    }

    public void setDataBidding(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> pickSelectEntities, int defItem) {
        categoryAdapter.setDatas(pickSelectEntities);
        categoryAdapter.setDefItem(defItem);
    }

    public void setCateData(int pos1, int pos2, int pos3) {
//        this.pos1 = pos1;
        this.pos2 = pos2;
        this.pos3 = pos3;
        if(pos2 == 0){
            categorySecondNo = "";
        }
        if(pos3 == 0){
            categoryThirdNo = "";
        }
        llSecondCategory.setVisibility(View.VISIBLE);
        llThirdCategory.setVisibility(View.GONE);
        secondAdapter.setDefItem(pos2);
        if (pos2 > 0) {
            thirdAdapter.setDatas(secondList.get(pos2).getChildren());
            thirdAdapter.setDefItem(pos3);
            if ("机械分类".equals(tvSecondCategory.getText().toString())) {
                tvThirdCategory.setText("机械类型");
            }
            if ("设备分类".equals(tvSecondCategory.getText().toString())) {
                tvThirdCategory.setText("设备类型");
            }
            llThirdCategory.setVisibility(View.VISIBLE);
        }
    }

    public void setCategoryData(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> pickSelectEntities) {
        categoryList.addAll(pickSelectEntities);
        categoryAdapter.setDatas(categoryList);
        llCategory.setVisibility(View.VISIBLE);
    }

    private CategoryCallBack mCategoryCallBack;
    private CategoryCallBack1 mCategoryCallBack1;

    public void setCategoryCallBack(CategoryCallBack categoryCallBack) {
        mCategoryCallBack = categoryCallBack;
    }

    public void setCategoryCallBack1(CategoryCallBack1 categoryCallBack1) {
        mCategoryCallBack1 = categoryCallBack1;
    }

    public interface CategoryCallBack {
        void resultPosition(String posName1, String posName2, String posName3, int pos1, int pos2, int pos3);
    }
    public interface CategoryCallBack1 {
        void resultPosition1(String posName1, String posName2, String posName3, String posName4, int pos1, int pos2, int pos3, int pos4);
        void setChildData(String position);
    }
}
